﻿function load_type() {
    var obj = document.getElementById("device_type");
    //obj.options.length = 0;
    $.ajax({
        url: "Ajax/QueryDevice.aspx?typedevice=gettype",
        type: "post",
        dataType: "json",
        data: {},
        success: function (msg) {
            var count = 0;
            $(msg).each(function () {
                var option;
                var txt = this.name
                var val = this.id;

                option = new Option(txt, val);
                if (obj.options.length <= count)
                    obj.options.add(option);
                count++;
            })
        }
    })
}

$("document").ready(function () {
    $("#book").hide();
    load_type();
    //$("#device_type").onclick = load_type();
    $("#query_device").mouseup(function () {
        $("#loading_device").show();
        var begin_time = $("#device_begintime").val();
        var end_time = $("#device_endtime").val();
        var device_type = $("#device_type").val();
        if (begin_time == "")
            alert("请选择开始时间");
        else if (end_time == "")
            alert("请选择结束时间");
        else if (device_type == "")
            alert("请输入设备种类");
        else if (begin_time > end_time)
            alert("开始时间大于结束时间！");
        else {

            $("#book").show();
            $.ajax({
                url: "Ajax/QueryDevice.aspx?",
                type: "post",
                dataType: "json",
                data: { begin: begin_time, end: end_time, type: device_type },
                success: function (msg) {
                    $("#loading_device").hide();
                    $("table#device_table tr:gt(0)").remove();
                    $(msg).each(function () {
                        var id = this.id;
                        var button = "<input type='button' class='book_device' id=" + id + " value='预订'></input>";
                        var row = "<tr id='" + id + "_device'><td align='center'>" + this.id + "</td><td align='center'>" + this.device_name + "</td><td align=center>" + button + "</td></tr>";
                        var row_num = $("table#device_table tr").size() - 1;
                        $(row).insertAfter($("table#device_table tr:eq(" + row_num + ")"));
                    })
                    $("input[type='button'].book_device").mouseup(function () {
                        var user_id = $("#user_id").val();
                        var result;
                        var id = $(this).attr("id");
                        if (user_id == "")
                            alert("请输入待借用户ID");
                        else {
                            var emploee_id = $("#user_id").val();

                            $.ajax({
                                url: "Ajax/CheckEmploee.aspx",
                                type: "post",
                                datatype: "json",
                                data: { user_id: emploee_id },
                                success: function (msg) {

                                    result = parseInt(msg);
                                    if (result == 1) {

                                        $.ajax({
                                            url: "BookDevice.aspx",
                                            type: "post",
                                            data: { id: id, begin: begin_time, end: end_time, userid: user_id },
                                            success: function (msg) {
                                                alert(msg);
                                                $("tr#" + id + "_device").remove();
                                            }
                                        })
                                    }
                                    else
                                        alert("请输入正确的员工ID");

                                }
                            })




                        }

                    })
                }
            });
        }
    });
});